import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import vitePluginImp from 'vite-plugin-imp';
import lessToJS from 'less-vars-to-js';
import fs from 'fs';
import path from 'path';

// 将variables.less 转换成 json 格式
const themeVariables = lessToJS(
  fs.readFileSync(path.resolve(__dirname, './src/styles/variables.less'), 'utf8'),
);

// 将variables.less转换的json写入到ts文件，方便ts中使用
fs.writeFileSync(
  path.resolve(__dirname, './src/styles/variables.ts'),
  `export const theme = ${JSON.stringify(themeVariables, null, ' ')}`,
  // `${JSON.stringify(themeVariables, null, ' ')}`,
);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    vitePluginImp({
      libList: [
        {
          libName: "antd",
          style: (name) => `antd/es/${name}/style`,
        },
      ],
    })
  ],
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        modifyVars: themeVariables,
      },
    }
  },
  server: {
    host: '0.0.0.0', // true   
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  }
})
